package com.example.applets.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.applets.entity.Comment;
import com.example.applets.service.impl.CommentServiceImpl;
import com.example.applets.util.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 评论 前端控制器
 * </p>
 *
 * @author 云烟成雨丶
 * @since 2020-10-09
 */
@Controller
@RequestMapping("/applets/comment")
public class CommentController {
    @Autowired
    private CommentServiceImpl commentService;


    @RequestMapping("/page")
    @ResponseBody
    //分页
    public AjaxResult selectPage(@RequestParam("maxModifydate") String maxModifydate, @RequestParam("minModifydate") String minModifydate, @RequestParam("level") String level, @RequestParam(value = "pageNo", defaultValue = "1") int pageNo, @RequestParam(value = "pageSize", defaultValue = "5") int pageSize) {
        Page<Comment> page = new Page<>(pageNo, pageSize);
        QueryWrapper<Comment> wrapper = new QueryWrapper<>();
        if (!level.equals("")) {
            wrapper.eq("level", Integer.parseInt(level));
        }
        if (!minModifydate.equals("")) {
            wrapper.ge("modifydate", minModifydate);
        }
        if (!maxModifydate.equals("")) {
            wrapper.le("modifydate", maxModifydate);
        }
        List<Comment> list = commentService.page(page, wrapper).getRecords();
        int count = (commentService.count(wrapper) - 1) / pageSize + 1;
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("list", list);
        map.put("total", count);
        if (list.size() == 0)
            return AjaxResult.error("暂无数据");
        return AjaxResult.success(map);
    }

    @RequestMapping("/delete")
    @ResponseBody
    //逻辑删除
    public AjaxResult deleteProduct(@RequestParam("id") long id) {
        Comment comment = commentService.getById(id);
        comment.setIsdel(false);
        if (!commentService.updateById(comment))
            return AjaxResult.error("删除失败!");
        return AjaxResult.success("删除成功!");
    }

    @RequestMapping("/additional")
    //查找单个
    public String additional(@RequestParam("id") long id, Model model) {
        Comment comment = commentService.getById(id);
        model.addAttribute("c", comment);
        return "/KComment/Additional";
    }

    @RequestMapping("/doAdditional")
    @ResponseBody
    public AjaxResult goAdditional(@RequestParam("conent") String conent, @RequestParam("id") long id) {
        Comment comment = commentService.getById(id);
        String str = comment.getConent();
        if (str.contains(";")) {
            return AjaxResult.error("无法继续追加");
        }
        str = str + "," + conent + ";";
        comment.setConent(str);
        commentService.updateById(comment);
        return AjaxResult.success("追加成功!");
    }
}

